package cn.daniellee.plugin.ce;

import cn.daniellee.plugin.ce.command.EconomyCommand;
import cn.daniellee.plugin.ce.component.VaultAPI;
import cn.daniellee.plugin.ce.listener.BungeeListener;
import cn.daniellee.plugin.ce.listener.PlayerListener;
import cn.daniellee.plugin.ce.storage.MysqlStorage;
import cn.daniellee.plugin.ce.storage.Storage;
import cn.daniellee.plugin.ce.storage.StorageConverter;
import cn.daniellee.plugin.ce.storage.YamlStorage;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin;

import java.text.DecimalFormat;

public class CraftEconomy extends JavaPlugin {

	private static CraftEconomy instance;

	private boolean bungeecord;

	private String prefix;
	
	private boolean onlineMode;

	private Storage storage;

	public Economy economy;

	public DecimalFormat format = new DecimalFormat("#,###.##");

	@Override
	public void onEnable() {
		instance = this;

		if (loadConfig()) {
			getLogger().info(" ");
			getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> CraftEconomy Loaded <<<<<<<<<<<<<<<<<<<<<<<<");
			getLogger().info(">>>>> If you encounter any bugs, please contact author's QQ: 768318841 <<<<<");
			getLogger().info(" ");

			// 如果是BC服，启动BC通道
			if (bungeecord = Bukkit.spigot().getConfig().getBoolean("settings.bungeecord", false)) {
				this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
				this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new BungeeListener());
			}

			economy = new VaultAPI();
			getServer().getServicesManager().register(Economy.class, economy, this, ServicePriority.Normal);

			Bukkit.getPluginManager().registerEvents(new PlayerListener(), this);

			Bukkit.getPluginCommand("money").setExecutor(new EconomyCommand());
			Bukkit.getPluginCommand("balance").setExecutor(new EconomyCommand());
			Bukkit.getPluginCommand("balancetop").setExecutor(new EconomyCommand());
			Bukkit.getPluginCommand("pay").setExecutor(new EconomyCommand());
		}
	}

	public boolean loadConfig() {
		getLogger().info("Loading config...");
		if(!getDataFolder().exists()) getDataFolder().mkdirs();
		// 载入插件基本配置
		prefix = getConfig().getString("prompt-prefix", "&7[&6CraftEconomy&7] &3: &r");
		onlineMode = getConfig().getBoolean("setting.online-mode", false);
		// 加载数据
		if (storage instanceof MysqlStorage) {
			((MysqlStorage) storage).close();
		}
		storage = getConfig().getBoolean("storage.mysql.use", false) ? new MysqlStorage() : new YamlStorage();
		if (storage.initialize()) {
			getLogger().info("[CraftEconomy]Storage initialized.");
		} else {
			getLogger().info(" ");
			getLogger().info("[CraftEconomy]Initializing data store failed, please edit the config and reload the plugin.".replace("&", "§"));
			getLogger().info(" ");
			Bukkit.getPluginManager().disablePlugin(this);
			return false;
		}
		if (getConfig().getBoolean("storage.mysql.use", false) && getConfig().getBoolean("storage.mysql.convert", false)) {
			YamlStorage yamlStorage = new YamlStorage();
			if (yamlStorage.initialize()) {
				StorageConverter converter = new StorageConverter((MysqlStorage) storage, yamlStorage);
				converter.execute();
				getConfig().set("storage.mysql.convert", false);
				saveConfig();
				getLogger().info("[CraftEconomy]Successfully transferred Yaml data to Mysql.");
			} else {
				getLogger().info(" ");
				getLogger().info("[CraftEconomy]Yaml data store initialization failed, data conversion canceled.".replace("&", "§"));
				getLogger().info(" ");
			}
		}
		saveDefaultConfig();
		return true;
	}

	@Override
	public void onDisable() {
		if (bungeecord) {
			this.getServer().getMessenger().unregisterOutgoingPluginChannel(this, "BungeeCord");
			this.getServer().getMessenger().unregisterIncomingPluginChannel(this, "BungeeCord");
		}

		getServer().getServicesManager().unregister(economy);

		getLogger().info(" ");
		getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> CraftEconomy Unloaded <<<<<<<<<<<<<<<<<<<<<<<<");
		getLogger().info(" ");
	}

	public static CraftEconomy getInstance() {
		return instance;
	}

	public String getPrefix() {
		return prefix;
	}

	public boolean isBungeecord() {
		return bungeecord;
	}

	public boolean isOnlineMode() {
		return onlineMode;
	}

	public Storage getStorage() {
		return storage;
	}

	public Economy getEconomy() {
		return economy;
	}

	public DecimalFormat getFormat() {
		return format;
	}
}
